package com.glance.browser.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name="KnowledgeItem")
public class KnowledgeItem  implements Serializable {

	private static final long serialVersionUID = -3931385415692480796L;
	
	 @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER )
	    @JoinColumn(name="author_id")
	private Author author;
	 
	@ManyToMany(cascade = CascadeType.ALL)
		@JoinTable(name = "KnowledgeItemAssociation", 
			joinColumns = { @JoinColumn(name = "child_id", referencedColumnName = "item_id")}, 
			inverseJoinColumns = { @JoinColumn(name = "parent_id") })
	private Set<KnowledgeClass> knowledgeClasses = new HashSet<KnowledgeClass>();
	
	// Persistent Fields:
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "item_id")
    Long id;
    
    @Column(name = "title", length = 45)
    @Size(min=1)
    private String title;
    
    @Column(name = "type", length = 45)
    @Size(min=1)
    private String type;
    
    @Column(name = "description", length = 200)
    private String description;
    
    @Column(name = "language", length = 45)
    private String language;
    
    @Column(name = "publisher_date")
	@Temporal(TemporalType.DATE)
	@DateTimeFormat(style = "M-")
    private Date publisherDate;

    // Constructors:
    public KnowledgeItem() {
    }

    public KnowledgeItem(String title, String type, String description, String language, Date puDate, Author author) {
        this.title = title;
        this.type = type;
        this.description = description;
        this.language = language;
        this.publisherDate = puDate;
        this.author = author;
    }

    // String Representation:
    @Override
    public String toString() {
        return title;
    }

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public Date getPublisherDate() {
		return publisherDate;
	}

	public void setPublisherDate(Date publisherDate) {
		this.publisherDate = publisherDate;
	}

	public Author getAuthor() {
		return author;
	}

	public void setAuthor(Author author) {
		this.author = author;
	}

	public Set<KnowledgeClass> getKnowledgeClasses() {
		return knowledgeClasses;
	}

	public void setKnowledgeClass(Set<KnowledgeClass> knowledgeClasses) {
		this.knowledgeClasses = knowledgeClasses;
	}
}
